<?php
class Html {
	public function getSelect($aOption, $sDefault = null, $aArguments = array(), $aBefore = null, $aAfter = null) {
		$sHtml = '<select>';
		if (is_array ( $aArguments )) {
			foreach ( $aArguments as $sKey => $sRow ) {
				$sHtml = str_replace ( '>', ' ' . $sKey . '="' . $sRow . '">', $sHtml );
			}
		}
		
		if (is_array ( $aBefore )) {
			$sHtml .= $this->_getOption ( $aBefore, $sDefault );
		}
		
		if (is_array ( $aOption )) {
			$sHtml .= $this->_getOption ( $aOption, $sDefault );
		}
		
		if (is_array ( $aAfter )) {
			$sHtml .= $this->_getOption ( $aAfter, $sDefault );
		}
		$sHtml .= '</select>';
		return $sHtml;
	}
	
	public function getRadio($aOption, $sDefault = null, $aArguments = array()) {
		$sHtml = '';
		$sArguments = '';
		if (is_array ( $aArguments )) {
			foreach ( $aArguments as $sKey => $sRow ) {
				$sArguments .= ' ' . $sKey . '="' . $sRow . '"';
			}
		}
		foreach ( $aOption as $sName => $sValue ) {
			$sText = '';
			$sHtml .= '<label><input type="radio"';
			if (is_array ( $sValue )) {
				foreach ( $sValue as $k => $v ) {
					$sHtml .= ' ' . $k . '="' . $v . '"';
					if ($k == 'value' && $v == $sDefault) {
						$sHtml .= ' checked';
					}
					if (($sName == 'value' && ! $sText) || $sName = 'text') {
						$sText = $v;
					}
				}
			} else {
				$sHtml .= ' value="' . $sName . '"';
				if ($sName == $sDefault) {
					$sHtml .= ' checked';
				}
				$sText = $sValue;
			}
			$sHtml .= $sArguments . ' />' . $sText . '</label>';
		}
		return $sHtml;
	}
	
	public function getCheckbox($aOption, $aDefault = array(), $aArguments = array()) {
		if (! is_array ( $aDefault )) {
			$aDefault = array ($aDefault );
		}
		$sHtml = '';
		$sArguments = '';
		if (is_array ( $aArguments )) {
			foreach ( $aArguments as $sKey => $sRow ) {
				$sArguments .= ' ' . $sKey . '="' . $sRow . '"';
			}
		}
		foreach ( $aOption as $sName => $sValue ) {
			$sText = '';
			$sHtml .= '<label><input type="checkbox"';
			if (is_array ( $sValue )) {
				foreach ( $sValue as $k => $v ) {
					$sHtml .= ' ' . $k . '="' . $v . '"';
					if ($k == 'value' && in_array ( $v, $aDefault )) {
						$sHtml .= ' checked';
					}
					if (($sName == 'value' && ! $sText) || $sName = 'text') {
						$sText = $v;
					}
				}
			} else {
				$sHtml .= ' value="' . $sName . '"';
				if (in_array ( $sName, $aDefault )) {
					$sHtml .= ' checked';
				}
				$sText = $sValue;
			}
			$sHtml .= $sArguments . ' />' . $sText . '</label>';
		}
		return $sHtml;
	}
	
	private function _getOption($aOption, $sDefault) {
		$sHtml = '';
		foreach ( $aOption as $sKey => $sRow ) {
			if (is_array ( $sRow ) && ! isset ( $sRow ['attr'] ) && ! isset ( $sRow ['text'] )) {
				$sHtml .= '<optgroup label="' . $sKey . '">';
				$sHtml .= $this->_getOption ( $sRow, $sDefault );
				$sHtml .= '</optgroup>';
			} else {
				if (is_array ( $sDefault )) {
					if (in_array ( $sKey, $sDefault )) {
						$sHtml .= '<option value="' . $sKey . '" selected';
					} else {
						$sHtml .= '<option value="' . $sKey . '"';
					}
				} elseif ($sDefault !== null && $sDefault == $sKey) {
					$sHtml .= '<option value="' . $sKey . '" selected';
				} else {
					$sHtml .= '<option value="' . $sKey . '"';
				}
				if (is_array ( $sRow )) {
					$sHtml .= ' ' . $sRow ['attr'] . '>' . $sRow ['text'] . '</option>';
				} else {
					$sHtml .= '>' . $sRow . '</option>';
				}
			}
		}
		return $sHtml;
	}
	
	public function rgb2str($sRgb) {
		$aMatch = array ();
		if (preg_match ( '/(\d+)\D*(\d+)\D*(\d+)/', $sRgb, $aMatch )) {
			return sprintf ( '#%02X%02X%02X', $aMatch [1], $aMatch [2], $aMatch [3] );
		} else {
			return false;
		}
	}
}